package com.tourism.controller;


import com.tourism.core.common.enums.CommentStatusEnum;
import com.tourism.core.common.utils.Result;
import com.tourism.core.security.util.SecurityHolderUtils;
import com.tourism.domain.dto.comment.CommentApproveDTO;
import com.tourism.domain.dto.comment.CommentQueryDTO;
import com.tourism.domain.entity.Comment;
import com.tourism.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 评论管理控制类
 *
 * @author lucky
 */
@RequestMapping("/api/admin/comment")
@RestController
public class CommentController {

    @Autowired
    private CommentService commentService;

    @PostMapping("page")
    @PreAuthorize("hasAuthority('comment:page')")
    public Result<?> getPage(@RequestParam("pageIndex") Integer pageIndex, @RequestParam("pageSize") Integer pageSize, @RequestBody CommentQueryDTO commentQueryDTO) {
        return Result.success(commentService.getPage(pageIndex, pageSize, commentQueryDTO));
    }


    @PostMapping("save")
    @PreAuthorize("hasAuthority('comment:save')")
    public Result<Boolean> save(@RequestBody Comment comment) {
        comment.setUserId(SecurityHolderUtils.getUserId());
        return commentService.save(comment) ? Result.success() : Result.failure();
    }


    @DeleteMapping("delete")
    @PreAuthorize("hasAuthority('comment:delete')")
    public Result<Boolean> delete(@RequestBody List<Long> ids) {
        return commentService.removeBatchByIds(ids) ? Result.success() : Result.failure();
    }


    @GetMapping("/get/{attractionId}")
    @PreAuthorize("hasAuthority('comment:get')")
    public Result<?> get(@PathVariable Long attractionId) {
        CommentQueryDTO comment = new CommentQueryDTO();
        comment.setStatus(CommentStatusEnum.APPROVED.getCode());
        comment.setAttractionId(attractionId);
        return Result.success(commentService.getPage(1, -1, comment));
    }


    @PutMapping("/approve")
    @PreAuthorize("hasAuthority('comment:approve')")
    public Result<Boolean> approve(@RequestBody CommentApproveDTO commentApproveDTO) {
        return commentService.approve(commentApproveDTO) ? Result.success() : Result.failure();
    }
}
